<script setup>
import { ref, reactive } from "vue";
import { ArrowDown } from "@element-plus/icons-vue";
import {
  getAdminList,
  addAdmin,
  getRoleDrop,
  editAdmin,
  delAdmin,
  changeAdminStatus,
} from "@/api/role";
import { ElMessage } from "element-plus";
import DialogForm from "./component/dialogForm.vue";
import Pagination from "@/components/Pagination/index.vue";

const searchForm = reactive({
  name: "",
  page: 1,
});
const loading = ref(false);
const dialogConfig = reactive({
  type: "add",
  titleList: {
    add: { text: "添加账号" },
    edit: { text: "编辑账号" },
  },
  visible: false,
});
const data = ref({});

const tempData = ref();

const roleGroupList = ref();
getList();
async function getList() {
  loading.value = true;
  const res = await getAdminList(searchForm);
  loading.value = false;
  data.value = res.data;
}
getRoleDropFn();
async function getRoleDropFn() {
  const res = await getRoleDrop();
  roleGroupList.value = res.data;
}
async function addAdminFn() {
  dialogConfig.visible = true;
  dialogConfig.type = "add";
}
async function editAdminFn(row) {
  dialogConfig.visible = true;
  dialogConfig.type = "edit";
  tempData.value = row;
}

async function dialogFormSubmit(e) {
  if (dialogConfig.type == "add") {
    const res = await addAdmin(e);
    if (res.code == 200) {
      ElMessage.success("添加成功");
      dialogConfig.visible = false;
      getList();
    }
  }
  if (dialogConfig.type == "edit") {
    const res = await editAdmin(e);
    if (res.code == 200) {
      ElMessage.success("修改成功");
      dialogConfig.visible = false;
      getList();
    }
  }
}
async function delAdminFn(row) {
  const res = await delAdmin({ id: row.id });
  if (res.code == 200) {
    ElMessage.success("删除成功");
    dialogConfig.visible = false;
    getList();
  }
}
const multiDelArr = ref([]);
const handleSelectionChange = (val) => {
  multiDelArr.value = val.map((item) => item.id);
};
async function changeAdmin(type) {
  if(multiDelArr.value.length == 0){
    ElMessage.warning("请选择要操作的数据");
    return;
  }
  const res = await changeAdminStatus({ id: multiDelArr.value.toString(), status: type });
  if (res.code == 200) {
    ElMessage.success("修改成功");
    getList();
  }
}

function changePagination(e) {
  searchForm.page = e.page;
  getList();
}
</script>

<template>
  <div class="container">
    <p class="title">管理员管理</p>
    <div class="box mb-2">
      <div class="search">
        <div class="cell">
          <span>账号：</span>
          <el-input style="width: 150px" v-model="searchForm.name" />
        </div>
        <div class="cell">
          <span>状态：</span>
          <el-select
            style="width: 150px"
            v-model="searchForm.status"
            placeholder="请选择"
          >
            <el-option label="全部" :value="0" />
            <el-option label="启用" :value="1" />
            <el-option label="禁用" :value="2" />
          </el-select>
        </div>
        <div class="cell">
          <el-button type="primary" @click="getList()">查询</el-button>
          <el-button>重置</el-button>
          <el-button @click="addAdminFn" class="mr-1" type="primary">添加账号</el-button>
          <el-dropdown trigger="click">
            <el-button type="primary">
              批量操作<el-icon class="el-icon--right"><arrow-down /></el-icon>
            </el-button>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="changeAdmin(1)">启用</el-dropdown-item>
                <el-dropdown-item @click="changeAdmin(2)">禁用</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
    </div>

    <div class="box">
      <el-table
        @selection-change="handleSelectionChange"
        v-loading="loading"
        border
        :data="data.data"
        style="width: 100%"
      >
        <el-table-column type="selection" width="38" />
        <el-table-column label="ID" width="120">
          <template #default="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column prop="account" label="账号" width="120" />
        <el-table-column prop="real_name" label="姓名/备注" />
        <el-table-column prop="roles[0].name" label="角色" width="120" />
        <el-table-column prop="created_at" label="创建时间" />
        <el-table-column prop="last_login" label="最后登录时间" />
        <el-table-column prop="status" label="状态" width="120">
          <template #default="scope">{{
            scope.row.status == 1 ? "启用" : "禁用"
          }}</template>
        </el-table-column>
        <el-table-column fixed="right" prop="status" label="操作" width="180">
          <template #default="scope">
            <el-button type="primary" @click="editAdminFn(scope.row)">编辑</el-button>
            <el-popconfirm title="确定删除?" @confirm="delAdminFn(scope.row)">
              <template #reference>
                <el-button type="warning">删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        @pagination="changePagination"
        :total="data.total"
        :page="searchForm.page"
      />
    </div>
    <!-- 弹窗 -->
    <el-dialog
      v-model="dialogConfig.visible"
      :title="dialogConfig.titleList[dialogConfig.type].text"
    >
      <DialogForm
        @submit="dialogFormSubmit"
        :dialogConfig="dialogConfig"
        :roleGroupList="roleGroupList"
        :echoData="tempData"
      />
    </el-dialog>
  </div>
</template>
<style scoped>
.search {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
}
.cell {
  display: flex;
  align-items: center;
  margin-right: 10px;
  span {
    white-space: nowrap;
    transform: translateY(-2px);
  }
}
</style>
